package com.xiguaji.tiktok.v1.ins.favorite.controller;

import com.xiguaji.tiktok.config.exception.ParamsException;
import com.xiguaji.tiktok.utils.Ret;
import com.xiguaji.tiktok.v1.InsBaseController;
import com.xiguaji.tiktok.v1.ins.favorite.dto.FavoriteTypeEnum;
import com.xiguaji.tiktok.v1.ins.favorite.model.InsFavorite;
import com.xiguaji.tiktok.v1.ins.favorite.service.InsFavoriteService;
import com.xiguaji.tiktok.v1.ins.favorite.vo.PageVo;
import com.xiguaji.tiktok.v1.tiktok.permission.Limit;
import com.xiguaji.tiktok.v1.tiktok.permission.RequestLimit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(value = "ins收藏",tags = {"ins收藏"})
public class FavoriteController extends InsBaseController {

    @Autowired
    InsFavoriteService insFavoriteService;

    @ApiOperation(value = "收藏播主")
    @RequestLimit(value = "InsBloggerFavorite",validateType = Limit.INS_BLOGGER_FAVORITE)
    @RequestMapping(value = "fa/blogger", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String collectBlogger(@RequestBody String body) throws ParamsException {
        Long uid = getParam(Long.class,body,"uid");
        InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_BLOGGER.getCode(),uid,(int)getUser().getId());

        if(null != insFavorite){
            // return Ret.success("已收藏过该播主",bloggerCollection.getId());
            return Ret.success("Added to favorite already",insFavorite.getId());
        }
        insFavorite = new InsFavorite();
        insFavorite.setType(FavoriteTypeEnum.INS_BLOGGER.getCode());
        insFavorite.setUserId((int)getUser().getId());
        insFavorite.setTargetId(uid);
        insFavorite.setCreateTime(System.currentTimeMillis()/1000);
        insFavoriteService.save(insFavorite);
        Map<String,Object> data = new HashMap<>();
        data.put("collectId",insFavorite.getId());
        // return Ret.success("收藏成功",data);
        return Ret.success("Added Successfully!",data);
    }

    @ApiOperation(value = "取消收藏播主")
    @RequestMapping(value = "fa/blogger", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public String cancelCollectBlogger(@RequestBody String body) throws ParamsException {
        int collectId = getParam(Integer.class,body,"collectId");
        insFavoriteService.delFavorite(collectId);
        // return Ret.success("OK");
        return Ret.success("Remove from favorites Successfully!");
    }

    @ApiOperation(value = "获取收藏播主列表")
    @RequestMapping(value = "fa/blogger", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String getCollectBloggerList(String data) throws UnsupportedEncodingException, ParamsException {
        val pageVo = getParam(PageVo.class, URLDecoder.decode(data, "UTF-8"));
        val bloggerPage = insFavoriteService.findBloggerPage(pageVo,getUser().getId());
        return Ret.success("SUCCESS",bloggerPage);
    }

    @ApiOperation(value = "收藏商品")
    @RequestLimit(value = "InsProductFavorite",validateType = Limit.INS_PRODUCT_FAVORITE)
    @RequestMapping(value = "fa/product", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String collectProduct(@RequestBody String body) throws ParamsException {
        Long uid = getParam(Long.class,body,"pid");
        InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_PRODUCT.getCode(),uid,(int)getUser().getId());

        if(null != insFavorite){
            // return Ret.success("已收藏过该播主",bloggerCollection.getId());
            return Ret.success("Added to favorite already",insFavorite.getId());
        }
        insFavorite = new InsFavorite();
        insFavorite.setType(FavoriteTypeEnum.INS_PRODUCT.getCode());
        insFavorite.setUserId((int)getUser().getId());
        insFavorite.setTargetId(uid);
        insFavorite.setCreateTime(System.currentTimeMillis()/1000);
        insFavoriteService.save(insFavorite);
        Map<String,Object> data = new HashMap<>();
        data.put("collectId",insFavorite.getId());
        // return Ret.success("收藏成功",data);
        return Ret.success("Added Successfully!",data);
    }

    @ApiOperation(value = "取消收藏商品")
    @RequestMapping(value = "fa/product", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public String cancelCollectProduct(@RequestBody String body) throws ParamsException {
        int collectId = getParam(Integer.class,body,"collectId");
        insFavoriteService.delFavorite(collectId);
        // return Ret.success("OK");
        return Ret.success("Remove from favorites Successfully!");
    }

    @ApiOperation(value = "获取商品收藏列表")
    @RequestMapping(value = "fa/product", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String getCollectProductList(String data) throws UnsupportedEncodingException, ParamsException {
        val pageVo = getParam(PageVo.class, URLDecoder.decode(data, "UTF-8"));
        return Ret.success("SUCCESS",insFavoriteService.findProductPage(pageVo,getUser().getId()));
    }

    @ApiOperation(value = "收藏帖子")
    @RequestLimit(value = "InsMediaFavorite",validateType = Limit.INS_MEDIA_FAVORITE)
    @RequestMapping(value = "fa/media", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String collectMedia(@RequestBody String body) throws ParamsException {
        Long uid = getParam(Long.class,body,"mid");
        InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_MEDIA.getCode(),uid,(int)getUser().getId());

        if(null != insFavorite){
            // return Ret.success("已收藏过该播主",bloggerCollection.getId());
            return Ret.success("Added to favorite already",insFavorite.getId());
        }
        insFavorite = new InsFavorite();
        insFavorite.setType(FavoriteTypeEnum.INS_MEDIA.getCode());
        insFavorite.setUserId((int)getUser().getId());
        insFavorite.setTargetId(uid);
        insFavorite.setCreateTime(System.currentTimeMillis()/1000);
        insFavoriteService.save(insFavorite);
        Map<String,Object> data = new HashMap<>();
        data.put("collectId",insFavorite.getId());
        // return Ret.success("收藏成功",data);
        return Ret.success("Added Successfully!",data);
    }

    @ApiOperation(value = "取消收藏帖子")
    @RequestMapping(value = "fa/media", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public String cancelCollectMedia(@RequestBody String body) throws ParamsException {
        int collectId = getParam(Integer.class,body,"collectId");
        insFavoriteService.delFavorite(collectId);
        // return Ret.success("OK");
        return Ret.success("Remove from favorites Successfully!");
    }

    @ApiOperation(value = "获取帖子收藏列表")
    @RequestMapping(value = "fa/media", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String getCollectMediaList(String data) throws UnsupportedEncodingException, ParamsException {
        val pageVo = getParam(PageVo.class, URLDecoder.decode(data, "UTF-8"));
        return Ret.success("SUCCESS",insFavoriteService.findMediaPage(pageVo,getUser().getId()));
    }

    @ApiOperation(value = "收藏品牌")
    @RequestLimit(value = "InsBrandFavorite",validateType = Limit.INS_BRAND_FAVORITE)
    @RequestMapping(value = "fa/brand", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String collectBrand(@RequestBody String body) throws ParamsException {
        Long uid = getParam(Long.class,body,"bid");
        InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_BRAND.getCode(),uid,(int)getUser().getId());

        if(null != insFavorite){
            // return Ret.success("已收藏过该播主",bloggerCollection.getId());
            return Ret.success("Added to favorite already",insFavorite.getId());
        }
        insFavorite = new InsFavorite();
        insFavorite.setType(FavoriteTypeEnum.INS_BRAND.getCode());
        insFavorite.setUserId((int)getUser().getId());
        insFavorite.setTargetId(uid);
        insFavorite.setCreateTime(System.currentTimeMillis()/1000);
        insFavoriteService.save(insFavorite);
        Map<String,Object> data = new HashMap<>();
        data.put("collectId",insFavorite.getId());
        // return Ret.success("收藏成功",data);
        return Ret.success("Added Successfully!",data);
    }

    @ApiOperation(value = "取消收藏品牌")
    @RequestMapping(value = "fa/brand", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public String cancelCollectBrand(@RequestBody String body) throws ParamsException {
        int collectId = getParam(Integer.class,body,"collectId");
        insFavoriteService.delFavorite(collectId);
        // return Ret.success("OK");
        return Ret.success("Remove from favorites Successfully!");
    }

    @ApiOperation(value = "获取品牌收藏列表")
    @RequestMapping(value = "fa/brand", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String getCollectBrandList(String data) throws UnsupportedEncodingException, ParamsException {
        val pageVo = getParam(PageVo.class, URLDecoder.decode(data, "UTF-8"));
        return Ret.success("SUCCESS",insFavoriteService.findBrandPage(pageVo,getUser().getId()));
    }
}
